/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.Task;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Collection;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CompleteTaskInstance
/*     */   extends CalendarRequest
/*     */ {
/*  48 */   private static final String[] TARGET_PATH = { "id" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request) {
/*  51 */     return TARGET_PATH;
/*     */   }
/*     */   
/*  54 */   protected boolean checkMountpointProxy(Element request) { return false; }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  58 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  59 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  60 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  62 */     ItemId iid = new ItemId(request.getAttribute("id"), zsc);
/*  63 */     Element exceptElem = request.getElement("exceptId");
/*     */     
/*  65 */     mbox.lock.lock();
/*     */     try {
/*  67 */       CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
/*  68 */       if (calItem == null) {
/*  69 */         throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Could not find calendar item");
/*     */       }
/*  71 */       if (!(calItem instanceof Task)) {
/*  72 */         throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Calendar item is not a task");
/*     */       }
/*  74 */       Invite inv = calItem.getDefaultInviteOrNull();
/*  75 */       if (inv == null) {
/*  76 */         throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "No default invite found");
/*     */       }
/*  78 */       if (!inv.isRecurrence()) {
/*  79 */         throw ServiceException.INVALID_REQUEST("Task is not a recurring task", null);
/*     */       }
/*     */       
/*  82 */       ParsedDateTime recurStart = inv.getStartTime();
/*  83 */       if (recurStart == null) {
/*  84 */         throw ServiceException.INVALID_REQUEST("Recurring task is missing start time", null);
/*     */       }
/*     */       
/*     */ 
/*  88 */       TimeZoneMap tzmap = inv.getTimeZoneMap();
/*  89 */       Element tzElem = request.getOptionalElement("tz");
/*  90 */       ICalTimeZone tz = null;
/*  91 */       if (tzElem != null) {
/*  92 */         tz = CalendarUtils.parseTzElement(tzElem);
/*  93 */         tzmap.add(tz);
/*     */       }
/*  95 */       ParsedDateTime exceptDt = CalendarUtils.parseDateTime(exceptElem, tzmap);
/*     */       
/*  97 */       if (exceptDt.getUtcTime() != recurStart.getUtcTime()) {
/*  98 */         throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
/*     */       }
/*     */       
/*     */ 
/* 102 */       Invite completed = createCompletedInstanceInvite(inv, exceptDt);
/* 103 */       mbox.addInvite(octxt, completed, calItem.getFolderId());
/*     */       
/*     */ 
/* 106 */       long oldStart = recurStart.getUtcTime();
/* 107 */       long newStart = -1L;
/* 108 */       Collection<CalendarItem.Instance> instances = calItem.expandInstances(oldStart, Long.MAX_VALUE, false);
/* 109 */       for (CalendarItem.Instance inst : instances) {
/* 110 */         if (inst.getStart() > oldStart) {
/* 111 */           newStart = inst.getStart();
/* 112 */           break;
/*     */         }
/*     */       }
/* 115 */       if (newStart != -1L)
/*     */       {
/* 117 */         ParsedDateTime newStartDt = ParsedDateTime.fromUTCTime(newStart);
/* 118 */         newStartDt.toTimeZone(inv.getStartTime().getTimeZone());
/* 119 */         newStartDt.setHasTime(recurStart.hasTime());
/*     */         
/* 121 */         ParsedDuration dur = inv.getEffectiveDuration();
/* 122 */         if (dur != null) {
/* 123 */           ParsedDateTime due = newStartDt.add(dur);
/* 124 */           inv.setDtEnd(due);
/*     */         }
/* 126 */         inv.setDtStart(newStartDt);
/* 127 */         inv.setSeqNo(inv.getSeqNo() + 1);
/* 128 */         inv.setDtStamp(System.currentTimeMillis());
/* 129 */         mbox.addInvite(octxt, inv, calItem.getFolderId());
/*     */       }
/*     */       else {
/* 132 */         mbox.delete(octxt, calItem.getId(), calItem.getType());
/*     */       }
/*     */     } finally {
/* 135 */       mbox.lock.release();
/*     */     }
/*     */     
/*     */ 
/* 139 */     Element response = getResponseElement(zsc);
/* 140 */     return response;
/*     */   }
/*     */   
/*     */   private Invite createCompletedInstanceInvite(Invite recur, ParsedDateTime dtStart) throws ServiceException {
/* 144 */     Invite inst = new Invite(MailItem.Type.TASK, recur.getMethod(), recur.getTimeZoneMap() != null ? recur.getTimeZoneMap().clone() : null, recur.isOrganizer());
/*     */     
/*     */ 
/* 147 */     long now = System.currentTimeMillis();
/*     */     
/*     */ 
/* 150 */     String uid = LdapUtil.generateUUID();
/* 151 */     inst.setUid(uid);
/* 152 */     inst.setSeqNo(0);
/*     */     
/*     */ 
/* 155 */     inst.setStatus("COMP");
/* 156 */     inst.setPercentComplete("100");
/* 157 */     inst.setCompleted(now);
/*     */     
/*     */ 
/* 160 */     inst.setDtStart(dtStart);
/* 161 */     ParsedDuration dur = recur.getEffectiveDuration();
/* 162 */     if (dur != null) {
/* 163 */       ParsedDateTime due = dtStart.add(dur);
/* 164 */       inst.setDtEnd(due);
/*     */     }
/* 166 */     inst.setDtStamp(now);
/*     */     
/*     */ 
/* 169 */     inst.setRecurrence(null);
/* 170 */     inst.setRecurId(null);
/*     */     
/*     */ 
/* 173 */     inst.setPriority(recur.getPriority());
/* 174 */     inst.setOrganizer(recur.getOrganizer());
/* 175 */     List<ZAttendee> attendees = recur.getAttendees();
/* 176 */     for (ZAttendee at : attendees)
/* 177 */       inst.addAttendee(at);
/* 178 */     inst.setName(recur.getName());
/* 179 */     inst.setLocation(recur.getLocation());
/* 180 */     inst.setFlags(recur.getFlags());
/* 181 */     inst.setDescription(recur.getDescription(), recur.getDescriptionHtml());
/* 182 */     inst.setFragment(recur.getFragment());
/*     */     
/* 184 */     return inst;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CompleteTaskInstance.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */